public class MyListNode {
    static class ListNode {
        int value;
        public ListNode next;

        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;

    public void createlist() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }

    public void addFirst(int data) {
       ListNode node = new ListNode(data);
       node.next = head;
       head = node;
   }
    public void addLast(int data) {
        ListNode cur = head;
        ListNode node = new ListNode(data);
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
   }
    public void remove(int key) {
    ListNode cur = head;
    if(cur.value==key){
        head=head.next;
    }
    while(cur.next != null){
        if(cur.next.value==key){
           ListNode del = cur;
           cur.next = del.next;
            return;
        }
        cur = cur.next;
    }
    }
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curr = cur.next;
            cur.next =null;
            cur = curr;
        }
        head = null;
        }
    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        ListNode prey = head;
        ListNode cur = head.next;
        while(cur !=null){
            if(cur.value == key){
                prey.next =cur.next;
                cur=cur.next;
            }
            else{
                prey =cur;
                cur =cur.next;
            }
        }
        if(head.value == key){
            head = head.next;
        }
    }

    public void addIndex(int index, int data) {
        if (index<0 && index>size()) {
        return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        ListNode cur = head;
      int count =0;
        while(count !=index-1){
            cur = cur.next;
            count++;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
   }
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if(cur.value == key){
                return true;
            }
            cur = cur.next;
        }
       return false;
    }
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
           return count;
       }

        public void display () {
            ListNode cur = head;
            while (cur.next != null) {
                System.out.println(cur.value + " ");
                cur = cur.next;
            }
            System.out.println(cur.value);
        }
        public static void main (String[]args){
            MyListNode myListNode = new MyListNode();
            myListNode.createlist();
            myListNode.display();
            System.out.println(myListNode.size());
            System.out.println("===========================");
            myListNode.display();
            System.out.println("==============================");
myListNode.removeAllKey(2);
myListNode.display();
            System.out.println("=================");
myListNode.clear();

        }
}




